package com.cheyunkeji.er.activity.fast_evaluate;

import android.content.Intent;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.Constants;
import com.cheyunkeji.er.adapter.fastevaluate.PalateLicenseDatePickerAdapter;
import com.cheyunkeji.er.base.BaseActivity;
import com.cheyunkeji.er.view.TopBar;
import com.cheyunkeji.er.view.fast_evaluate.YearAndMonthPickerView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class VehicleLicenseDatePickActivity extends BaseActivity implements YearAndMonthPickerView.OnCurrentListItemChecked {


    private static final String TAG = VehicleLicenseDatePickActivity.class.getSimpleName() ;
    @BindView(R.id.v_topbar)
    TopBar vTopbar;
    @BindView(R.id.lv_date_picker)
    ListView lvDatePicker;
    @BindView(R.id.activity_vehicle_license_date_pick)
    LinearLayout activityVehicleLicenseDatePick;

    private ArrayList<String> yearList ;
    private PalateLicenseDatePickerAdapter adapter ;

    private String[] startEndYear ;

    private int lastCheckedItemPosition = -1;

    @Override
    protected void init() {
        setContentView(R.layout.er_activity_vehicle_license_date_pick);
        ButterKnife.bind(this);

        initDatList();

    }

    private void initDatList() {

        if(null != getIntent().getStringArrayExtra(Constants.CAR_MODEL_START_END_YEAR_ARRAY)){
            startEndYear = getIntent().getStringArrayExtra(Constants.CAR_MODEL_START_END_YEAR_ARRAY);
        }

        yearList = new ArrayList<>();

        int startYear = Integer.valueOf(startEndYear[0]);
        int endYear = Integer.valueOf(startEndYear[1]);

        for(int i = endYear ; i >= startYear ;i--){
            yearList.add(String.valueOf(i));
        }
        adapter = new PalateLicenseDatePickerAdapter(yearList,this);

    }

    @Override
    protected void initView() {

        vTopbar.setTitle("首次上牌日期");
        vTopbar.setLeftBack();
        lvDatePicker.setAdapter(adapter);

    }

    @Override
    protected void initData() {

    }

    @Override
    public void notifyListClearOthersCheck(int attachedToListItemPosition, int selectedMonth) {

        Intent intent = new Intent();
        intent.putExtra(Constants.CAR_PALATE_YEAR_AND_MONTH,yearList.get(attachedToListItemPosition)+"-"+selectedMonth);
        setResult(Constants.RESULT_CODE_OK,intent);
        finish();
    }


//    @Override
//    public void notifyListClearOthersCheck(int attachedToListItemPosition) {

//        ToastUtil.showToast(""+attachedToListItemPosition);
//        ToastUtil.showToast(lvDatePicker.getChildAt(attachedToListItemPosition).getClass().getSimpleName());
//        ToastUtil.showToast(lvDatePicker.getAdapter().getCount()+"");

//        ((YearAndMonthPickerView)lvDatePicker.getChildAt(attachedToListItemPosition)).clearCheck();

//        for(int i = 0; i < lvDatePicker.getChildCount() ;i++){
//           if(attachedToListItemPosition != i)
//            ((YearAndMonthPickerView)lvDatePicker.getChildAt(attachedToListItemPosition)).setCheckedToMonth12();
//        }

//        if(lastCheckedView !=null){
//          lastCheckedView.clearCheck();
//        }

//        Log.e(TAG, "notifyListClearOthersCheck:  lastCheckedItemPosition "+lastCheckedItemPosition);

//        if(lastCheckedItemPosition != -1 && lvDatePicker.getChildAt(attachedToListItemPosition) != null){
//            ((YearAndMonthPickerView) lvDatePicker.getChildAt(attachedToListItemPosition)).setMyOnCheckedChangeListener(null);
//            ((YearAndMonthPickerView) lvDatePicker.getChildAt(attachedToListItemPosition)).clearCheck();
//            ((YearAndMonthPickerView) lvDatePicker.getChildAt(attachedToListItemPosition)).
//                    setMyOnCheckedChangeListener(new OnMonthCheckedChangeListener(lastCheckedItemPosition,VehicleLicenseDatePickActivity.this));
//        }
//        lastCheckedItemPosition = attachedToListItemPosition ;

//      lastCheckedView = (YearAndMonthPickerView) lvDatePicker.getChildAt(attachedToListItemPosition);

//    }
}
